/**
 * 
 */
package com.biomedica.statemachine;

import java.util.ArrayList;
import java.util.List;

/**
 * @author HALOBYDI
 * This is an abstract class that will implement the State Object Factory
 *
 */
public abstract class State {
	/** Construct a new State Object **/
	public State(StateContext context)
	{
		TriggerList=new ArrayList<Triggers>();
		this.setStateEngineListener(context.getStateMachineEngine());
		
	}
	/** Construct a new State Object **/
	public State(StateContext context,String Name)
	{
		TriggerList=new ArrayList<Triggers>();
		this.setStateEngineListener(context.getStateMachineEngine());
		this.Name=Name;
	}
	/** Construct a new State Object **/
	public State(StateContext context,String Name,int id)
	{
		TriggerList=new ArrayList<Triggers>();
		this.setStateEngineListener(context.getStateMachineEngine());
		this.Name=Name;
		this.Id=id;
	}
	////////////////////////////////////////////////////////
	/** State Textual Name **/
	private String Name;	
	public void setName(String name)
	{
		Name=name;
	}
	public String getName()
	{
		return Name;
	}
	/////////////////////////////////////////////////////////
	/** State Id **/
	private int Id;
	public void setId(int id)
	{
		Id=id;
	}
	public int getId()
	{
		return Id;
	}
	
	////////////////////////////////////////////////////////////
	/** Text Discribing the state **/
	private String Text;
	public void setText(String t)
	{
		Text=t;
	}
	public String getText()
	{
		return Text;
	}
	///////////////////////////////////////////////////////////
	private List<Triggers> TriggerList=null;
	///////////////////////////////////////////////////////////
	/** Add a new Triger Object to the trigger list the trigger will be evaluated in the order that they have been added **/
	public void Add(Triggers t)
	{
		this.TriggerList.add(t);
	}
	//////////////////////////////////////////////////////////
	private StateActions actions;
	/** set the delegate that will be called when the State is active (Do,Enter and Exit) **/
	public void setStateAction(StateActions a)
	{
		actions=a;
	}
   ///////////////////////////////////////////////////////////
	/** called internaly when the Do Statement is called by the egine **/
	public void OnDo(StateContext context)
	{
		actions.OnDo(context);
	}
	/** called internaly when the Enter Statement is called by the egine **/
	public void OnEnter(StateContext context)
	{
		actions.OnEnter(context);
	}
	/** called internaly when the Exit Statement is called by the egine **/
	public void OnExit(StateContext context)
	{
		actions.OnExit(context);
	}
	/////////////////////////////////////////////////////////
	private StateEngineListener EngineListener;
	/** Set the State Engine Listner**/
	public void setStateEngineListener(StateEngineListener e)
	{
		EngineListener=e;
	}
	
	//////////////////////////////////////////////////////////
	/** Test The state **/
	public void Test(StateContext context)
	{
		for(Triggers s : TriggerList)
		{
			if(s.IsValid(context))
			{
				OnExit(context);
				this.EngineListener.onChangeState(s.getNextState());
			}
		}
	}
}
